package com.gemme.gemme.shequ;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.MainActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.login.GlideCircleTransform;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.util.StrUtil;
import com.gemme.gemme.view.GeneralDialog;
import com.gemme.gemme.view.Topview;
import com.google.android.material.tabs.TabLayout;
import com.jaeger.library.StatusBarUtil;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class ShequFansActivity extends BaseActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shequ_fans);
        initTop();
        initView();
    }
    TabLayout tabLayout;
    SwipeRecyclerView mRecyclerView;
    Integer tabindex = 0;
    SqFansAdapter sqFansAdapter;
    private void initView() {
        mRecyclerView = findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));


        tabLayout = findViewById(R.id.tablayout);
        initTabLayout();

        //进来首先选择第一个
//        tabLayout.getTabAt(0).select();//进行选择第几个tab

        getDataS();
    }



    private void initTabLayout() {
//        ,"互相关注"
         String[] titles = new String[]{"粉丝","关注"};

        for(int i=0;i<titles.length;i++){
            tabLayout.addTab(tabLayout.newTab().setTag(i));
            tabLayout.getTabAt(i).setText(titles[i]);
        }

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                try {

                    tabindex = (Integer) tab.getTag();

                    getDataS();
                }catch (Exception e){

                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }


    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("用户名");
    }


    class SqFansAdapter extends RecyclerView.Adapter<SqFansAdapter.ViewHolder>{

        private List<FansBean.DataBean.RecordsBean> adapterList;

        public SqFansAdapter(List<FansBean.DataBean.RecordsBean> adapterList) {

            this.adapterList = adapterList;
        }

        public void setData(List<FansBean.DataBean.RecordsBean> adapterList) {
            this.adapterList = adapterList;
        }


        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.item_shequ_fans, null);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            inflate.setLayoutParams(layoutParams);
            return new SqFansAdapter.ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            FansBean.DataBean.RecordsBean recordsBean = adapterList.get(position);
            holder.fans_0_view.setVisibility(View.GONE);
            holder.fans_1_view.setVisibility(View.GONE);
            holder.fans_2_view.setVisibility(View.GONE);
           switch (tabindex){
               case 0:
                   holder.fans_0_view.setVisibility(View.VISIBLE);
                   initfans_0(holder.fans_0_view,recordsBean);
                   break;
               case 1:
                   holder.fans_1_view.setVisibility(View.VISIBLE);
                   initfans_1(holder.fans_1_view,recordsBean);
                   break;
               case 2:
                   holder.fans_2_view.setVisibility(View.VISIBLE);
                   break;
           }
        }

        private void initfans_1(View view, FansBean.DataBean.RecordsBean recordsBean) {
            try {
                ImageView iv_head = view.findViewById(R.id.iv_head);
                TextView tv_name = view.findViewById(R.id.tv_name);
                tv_name.setText(recordsBean.getNickName());
                String headImg = StrUtil.noNUll(recordsBean.getHeadImg());
                if(!TextUtils.isEmpty(headImg)){
                    Glide.with(mContext).load(headImg).transform(new GlideCircleTransform(mContext))
                            .into(iv_head);
                }

                view.findViewById(R.id.tv_guanzhued).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //取消关注
                        GeneralDialog dialog = new GeneralDialog();
                        dialog.showDialog(mContext);
                        dialog.setContent("确认不再关注?");

                        dialog.getTv_confirm().setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                toGuanzhu(recordsBean.getUserId() + "");
                                dialog.dismisss();
                            }
                        });

                    }
                });

            }catch (Exception e){

            }

        }


        private void initfans_0(View view, FansBean.DataBean.RecordsBean recordsBean) {

            try {
                ImageView iv_head = view.findViewById(R.id.iv_head);
                TextView tv_name = view.findViewById(R.id.tv_name);
                tv_name.setText(recordsBean.getNickName());
                String headImg = StrUtil.noNUll(recordsBean.getHeadImg());

                if(!TextUtils.isEmpty(headImg)){
                    Glide.with(mContext).load(headImg).transform(new GlideCircleTransform(mContext))
                            .into(iv_head);
                }

                findViewById(R.id.iv_guanzhu).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        toGuanzhu(recordsBean.getUserId() + "");
                    }
                });
            }catch (Exception e){

            }
        }

        @Override
        public int getItemCount() {
            return adapterList.size();
        }


        public class ViewHolder extends RecyclerView.ViewHolder {
            View fans_0_view;
            View fans_1_view;
            View fans_2_view;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                fans_0_view = itemView.findViewById(R.id.fans_0_view);
                fans_1_view = itemView.findViewById(R.id.fans_1_view);
                fans_2_view = itemView.findViewById(R.id.fans_2_view);
            }
        }
    }

    private void getDataS() {
//        类型 1 粉丝 2 关注
        int type = tabindex == 0 ? 1 : 2;
        String url = DataManager.BaseUrl + "community/appcusersfans/focusonlist?current=1&size=1000" +
                "&type=" + type;

        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(mContext));
        DataManager.toServer2(mContext, url, DataManager.POST_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {

                try {
                    FansBean fansBean = JSON.parseObject(response, FansBean.class);
                    List<FansBean.DataBean.RecordsBean> records = fansBean.getData().getRecords();
                    if(records == null)
                        return;
                    if(sqFansAdapter!= null){
                        sqFansAdapter.setData(records);

                        sqFansAdapter.notifyDataSetChanged();
                    }else{
                        sqFansAdapter = new SqFansAdapter(records);
                        mRecyclerView.setAdapter(sqFansAdapter);
                    }
                }catch (Exception e){

                }

                System.out.println("");
            }
        });
    }



    private void toGuanzhu(String userId) {
        try {
            String url = DataManager.BaseUrl + "community/appcusersfans/focuson?userId=" + userId;

            HashMap<String, String> headMap = new HashMap<>();
            headMap.put(DataManager.AuthorizationKey,DataManager.getToken(mContext));
            DataManager.toServer2(mContext, url, DataManager.POST_TYPE, "", headMap, new DataManager.GeResponse() {
                @Override
                public void onResponse(String response) {
                    try {

//                        getDataS();
//                        String msg = (new JSONObject(response)).getString("msg");
//                        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
                        getDataS();
                    }catch (Exception e){

                    }
                    System.out.println("");
                }
            });
        }catch (Exception e){

        }

    }



}